/**
 * Created by CZ on 2017/4/6.
 */
module.exports = function ($scope,httpfactory,$state,$stateParams,modelService,$localStorage) {
    var user = $localStorage.get('cloud-developer');
    var token = user?user.accessToken:'1';
    $scope.editFlag = false;
    $scope.NameTip = '';
    //获取parentId列表
    httpfactory.getParentIdList(token).then(function(response){
        console.log(response.data,response.data.length );
        $scope.parentIdList = [{wikiName:'默认', id:0}];
        if(response.data.length > 0){  //若返回数据为空
            $scope.parentIdList = $scope.parentIdList.concat(response.data);
        }
        console.log($scope.parentIdList);
        //初始化
        $scope.form = {
            wikiName:'',
            wikiNo:0,
            remark:'',
            parentId: $scope.parentIdList[0].id //默认选中第一个
        }
        // $scope.form.parentId = $scope.parentIdList[0].id;  //默认选中第一个

    },function(error){
        console.log(error);
    });

   //判断是否有传值
    if($stateParams.id){
       // console.log($stateParams.id);
        //获取单条数据
        httpfactory.getWiki($stateParams.id,token).then(function(response){
            console.log(response);
            $scope.form = {
                wikiName:response.data.wikiName,
                wikiNo:response.data.wikiNo,
                remark:response.data.remark,
                parentId:response.data.parentId
            }
        },function(error){
            console.log(error);
        });
    }
    $scope.ParentIdChange = function(parentId){
        $scope.form.parentId = parentId;
    };
   //提交数据
    $scope.submit = function(){
        console.log($scope.form);
        if($stateParams.id){  //修改数据
            httpfactory.resetWiki($stateParams.id,$scope.form,token).then(function(response){
                console.log(response);
                if(response.data.code == 0){
                    var page = 'DeveloperMain.Wiki';
                    modelService.openTipsModel('修改成功！',page);

                }
            },function(error){
                console.log(error);
            });
        }else {               //第一次提交
            httpfactory.addWiki($scope.form,token).then(function(response){
                console.log(response);
                if(response.data.code == 0){
                    var page = 'DeveloperMain.Wiki';
                    modelService.openTipsModel('提交成功！',page);
                }
            },function(error){
                console.log(error);
            });
        }



    };

    //检测标题是否重名
    $scope.CheckName = function(){
        if($scope.form.wikiName != ''){
            console.log($scope.form.wikiName);
            httpfactory.checkName($scope.form.wikiName,token).then(function(response){
                console.log(response);
                if(response.data == 'true'){
                    $scope.NameTip = '可使用标题';
                }else {
                    $scope.NameTip = '此标题已存在!';
                }

            },function(error){
                console.log(error);
            })
        }
    };

    // function openModel(content,page){
    //     $modal.open({
    //         animation: true,
    //         templateUrl: './views/model/alertModel.html',
    //         size: 'sm',
    //         controller: function($scope,$modalInstance,$timeout,$state) {
    //             $scope.content = content;
    //             $timeout(function(){
    //                 $modalInstance.close();
    //                 $state.go(page);
    //             },1000);
    //         }
    //     });
    // }

    
};